import {
	ApiGetAllActivity,
	ApiMembercouponSave
} from '@/common/request/api/activity.js'
import {
	SHOP_ACTIVITYLIST,
	SHOP_COUPONLIST
} from "../types.js"

const state = {
	shopActivityList:[],
	shopCouponList:[],
	loadingStatus:'loading',
}

const actions = {
	// 领取优惠券
	async ActMembercouponSave({dispatch},payload){
		const res = await ApiMembercouponSave(payload)
		if(res.success){
		}else{
			uni.showModal({
				title: '提示',
				content: res.msg,
				showCancel:false
			});
		}
		return res
	},
	// 获取全部店铺活动
	async ActGetAllActivity({commit,dispatch},payload){
		const res = await ApiGetAllActivity(payload)
		if(res.success){
			if(payload.isCoupon){
				commit('SHOP_COUPONLIST', res.data)
			}else{
				commit('SHOP_ACTIVITYLIST', res.data)
			}
		}else{
			uni.showModal({
				title: '提示',
				content: res.msg,
				showCancel:false
			});
		}
		return res
	},
}

const mutations = {
	// 店铺活动列表
	[SHOP_ACTIVITYLIST](state, data){
		data.map((item,index)=>{
			if(item.activityType == 10){
				let showDescription = ''
				item.activityRuleList.map((v,i)=>{
					showDescription += `满${v.limitWhere}元，立减${v.couponAmount}元；`
				})
				item.activityTypeName = '满减'
				item.showDescription = showDescription || item.activityDescription
			}else if(item.activityType == 30){
				item.activityTypeName = '满赠'
			}
		})
		state.shopActivityList = data
	},
	// 店铺优惠券列表
	[SHOP_COUPONLIST](state, data){
		state.shopCouponList = data
	},
}

const getters = {}

export default {
	state,
	mutations,
	actions,
	getters
}

